<?php

namespace addons\sumprint\controller;

use addons\oil\model\Order as OrderModel;
use think\addons\Controller;
use think\Log;

class Index extends Controller
{
//    // 开放平台平台公钥
//    protected static $rsa_public_key = '';
//    // 开放平台应用公匙
//    protected static $aes_encryption_key = '';
    /**
     * 扫码回调
     * @return void
     */
    public function callback()
    {
        $addon_config = get_addon_config('sumprint');
        $rsa_public_key = $addon_config['yilianyun']['rsa_public_key'];
        $aes_encryption_key = $addon_config['yilianyun']['aes_encryption_key'];

        $postData = $this->request->post();
//        Log::info(['print_back'=>$postData]);
        $message='';
        if ($postData) {
            $content = json_encode($postData);
//            Log::info("[推送参数]----[postData][$content]");
            $isVerify = self::verifySignature($postData['ciphertext'], $rsa_public_key, base64_decode($postData['signature']));
            if (!$isVerify) {
                $message = '验签失败';
            }
            $params = self::decode(base64_decode($postData['ciphertext']), $aes_encryption_key, $postData['iv'], base64_decode($postData['tag']));
            $paramsArr=json_decode($params,true);
            if (!$params) {
                $message = '解密失败';
            }
            ## todo 后续对接自己项目功能
            ## todo 对接打印机小票打印
            $scanned_code=$paramsArr['order_other']['scanned_code']??'';
            $scanned_code_arr=explode('|',$scanned_code);
            $order_sn=$scanned_code_arr[0]??'';
//          $version=$scanned_code_arr[1]??''; //核销端独立版本才需要
            if($order_sn){
                $order_q = new OrderModel();
                $order=$order_q->where('order_sn',$order_sn)->where('hexiao_status',0)->where('aftersale_status',0)->where('refund_status',0)->payedorfinish()->find();
                if($order){
                    $order->hexiao_status=1;
                    $order->ext = json_encode($order->setExt($order, ['hexiao_time' => time()]));      // 完成时间
                    $order->save();
                    $type='admin';
                    \addons\oil\model\OrderAction::operAdd($order, '', $type, '打印机核销订单','hexiao');
                    //核销后操作
                    $data = ['order' => $order];
                    \think\Hook::listen('order_hexiao_after', $data);
                }
            }
        }
        $return['message']='ok';
        $return['dataMsg']=$message;
        return json($return);
    }
    /**
     * Notes: 验证签名
     * @param $data
     * @param $privateKey
     */
    public static function verifySignature($data, $publicKey, $signature)
    {
        return openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA256);
    }
    /**
     * Notes: 解密
     * @param $string
     * @param $key
     * @param $iv
     * @param $tag
     * @param $additionalData
     * @return false|string
     */
    public static function decode($string, $key, $iv, $tag, $additionalData = 'transaction')
    {
        $decrypted = openssl_decrypt($string, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, $additionalData);
        $decrypted = base64_decode($decrypted);
        return $decrypted;
    }


}
